package froxy.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import froxy.handler.ProxyHandler;

public class HttpProxyServer implements Runnable {
    private static Log logger = LogFactory.getLog(HttpProxyServer.class);

    public void start() throws Exception {
        Server server = new Server();
        SelectChannelConnector connector0 = new SelectChannelConnector();
        connector0.setPort(Helper.getFroxyPort());
        connector0.setMaxIdleTime(Helper.getMaxIdleTime());
        connector0.setThreadPool(new QueuedThreadPool(Helper.getQueuedThreadPool()));
        ContextHandler context = new ContextHandler();
        context.setContextPath("/");
        context.setResourceBase(".");
        context.setClassLoader(Thread.currentThread().getContextClassLoader());
        context.setHandler(new ProxyHandler());
        server.setHandler(context);
        server.addConnector(connector0);

        // context.setHandler(new HelloHandler());
        server.start();
        logger.info("Froxy Started at port " + Helper.getFroxyPort());
        server.join();
    }

    public void run() {
        try {
            start();
        } catch (Exception e) {
            logger.error("Error ", e);
        }

    }
}
